package com.ezops.dtcc.model;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.xml.xpath.XPath;

import org.w3c.dom.Document;

public class FXSwap extends AckNack implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

	public FXSwap(Document doc, XPath xpath, String fileName) {
		this.doc = doc;
		this.xpath = xpath;
		this.fileName = fileName;
		
	}
	
	private Date validateDate(String dateTimeString) throws ParseException {
		try {
			if(dateTimeString != null && dateTimeString.length() > 0) {
				dateTimeString = dateTimeString.replace('T', ' ');
				dateTimeString = dateTimeString.replace('Z', ' ').trim();
				
				return sdf.parse(dateTimeString);
			}
		} catch(ParseException pe) {
			return sdf2.parse(dateTimeString);
		}
		return null;
	}

	@SuppressWarnings("unused")
	public void parse() throws Exception {
		//req
		source = "DTCC";
		repository = "";
	
		
		//req
		primary_asset_class = getXPath("primaryAssetClass");
		submitted_for_prefix = getXPath("sentBy");
	  
		//req  XXXXXXXXXXXXXXXXXXXXXXXX
		submitted_for_value = getXPath("onBehalfOf/partyReference/@href");
		
		//req
		product_id_prefix = getXPath("productId");
		//req
		product_id_value = getXPath("productId");
		
		//req
		message_id = getXPath("messageId");
		//req
		event_id = "NA" ; //getXPath("eventIdentifiers[@partyReference=Party1Alias]");
	
		
		//req
		usi_prefix = getXPath("issuer");
		//req
		usi_value = getXPath("tradeId", "tradeIdScheme");
		
		
		
		
		uti_prefix = getXPath("tradeId", "tradeIdScheme");
		uti = getXPath("issuer", "issuerIdScheme");
		
	
		prior_usi_prefix = getXPath("originatingTradeId", "tradeId");
		prior_usi_value = getXPath("issuer", "issuerId");
		
		prior_uti_prefix = getXPath("tradeId", "tradeId");
		prior_uti = getXPath("issuer", "issuerId");
	
		//req
		trade_party1_transaction_id = getXPath("tradeId");
		//req
		trade_party2_transaction_id = getXPath("tradeId");
		
		//req
		message_type = getXPath("reportingPurpose");
		
		//req  ?  
		action = "";
	
		//req
		transaction_type = getXPath("originatingEvent");
		
		trade_party1_role = getXPath("organizationType");
		
		trade_party1_prefix = getXPath("partyId");
		
		//req XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
		trade_party1_value = getXPath("partyId");
		
		trade_party2_role = getXPath("organizationType");
		trade_party2_prefix = getXPath("partyId");
		
		//req XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
		trade_party2_value = getXPath("partyId");
		
		//req
		String date = getXPath("executionDateTime");
		execution_timestamp = validateDate(date);
		
		date = getXPath("tradeDate");
		trade_date = validateDate(date);
		date = getXPath("unadjustedDate");
		effective_date = validateDate(date);
		verification_type = getXPath("verificationMethod");
		
		//req
		date = getXPath("confirmed");
		confirmation_datetime = validateDate(date);
		
		//req
		confirmation_type = getXPath("confirmationMethod");
		
		Date as_of_date = null;
		Date valuation_datetime = null;
		mandatory_clearing_indicator = getXPath("mandatorilyClearable");
		party1_reporting_obligation = getXPath("supervisoryBody");
		party2_reporting_obligation = getXPath("supervisoryBody");
		reporting_jurisdiction = getXPath("supervisoryBody");
		party1_voluntary_submission = getXPath("regulator", "reportingRole");
		party2_voluntary_submission = getXPath("regulator", "reportingRole");
		//req
		reason_code = "";
		reason_description = "";
		net_reporting_obligation = "";
		cftc_expected = "";
		cftc_actual = "";
		cftc_deviation = "";
		esma_expected = "";
		esma_actual = "";
		esma_deviation = "";
		fpml = "";

		insert_timestamp = Calendar.getInstance().getTime();
		date = getXPath("creationTimestamp");
		creation_timestamp = validateDate(date);
		
		reporting_purpose = getXPath("reportingPurpose");
	}
}
